<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Shixun extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index(){
		$data = json_decode(file_get_contents("http://123.206.137.187:804/HandlerData.ashx?action=GetZhangtaoInfo"),true);
		$shixun_list = array();
		if(!empty($data['rows']) && count($data['rows']) > 0){
			$this->load->model("Shixuns");
			$ids = '';
			foreach ($data['rows'] as $key => $value) {
				$use_code = get_field_by_where("o2o_school_system_shixun_codes", "COUNT(id) AS count", "sx_id='{$value['Id']}' AND school_id='{$this->user_data['school_id']}' AND status = 2");
				$value['use_code'] = $use_code['count'];
				$no_use_code = get_field_by_where("o2o_school_system_shixun_codes", "COUNT(id) AS count", "sx_id='{$value['Id']}' AND school_id='{$this->user_data['school_id']}' AND status = 1");
				$value['nouse_code'] = $no_use_code['count'];
				$shixun_list[$value['Id']] = $value;
				if($key == 0){
					$ids .= "'" . $value['Id'] . "'";
				}else{
					$ids .= ",'" . $value['Id'] . "'";
				}
			}
			$pack_list = $this->Shixuns->get_sx_packages($ids);
			if($pack_list){
				foreach ($pack_list as $k => $v) {
					if(isset($shixun_list[$v['sx_id']])){
						$shixun_list[$v['sx_id']]['status'] = $v['status'];
						$shixun_list[$v['sx_id']]['pack_id'] = $v['id'];
						$shixun_list[$v['sx_id']]['price'] = $v['price'];
						$shixun_list[$v['sx_id']]['on_time'] = $v['on_time'];
						$shixun_list[$v['sx_id']]['expire_day'] = $v['expire_day'];
						$shixun_list[$v['sx_id']]['commission'] = $v['commission'];
					}
				}
			}
		}
		// print_r($shixun_list);
		$this->smarty->assign("menu", "shelves");
		$this->smarty->assign("shixun_list", $shixun_list);
		$this->smarty->display("shixun/list.html");
	}

	function shelves($sx_id = 0){
		$post_data = $this->input->post();
		if($post_data){
			$where = "school_id='{$this->user_data['school_id']}' AND sx_id='{$post_data['sx_id']}'";
			$shelves_info = get_field_by_where("o2o_school_sx_packages", "id", $where);
			if($shelves_info){
				// echo json_encode(array("code" => 0));
				$post_data['status'] = 1;
				$this->db->where("id", $shelves_info['id']);
				$this->db->update("o2o_school_sx_packages", $post_data);
				echo json_encode(array("code" => 2));
			}else{
				$shelves_data['school_id'] = $this->user_data['school_id'];
				$shelves_data['sx_id'] = $post_data['sx_id'];
				$shelves_data['price'] = $post_data['price'];
				$shelves_data['status'] = 1;
				$shelves_data['commission'] = $post_data['commission'];
				$shelves_data['expire_day'] = $post_data['expire_day'];
				$shelves_data['on_time'] = time();
				$this->db->insert("o2o_school_sx_packages", $shelves_data);
				echo json_encode(array("code" => 1));
			}
		}else{
			if(!empty($sx_id)){
				$pack_info = get_field_by_where("o2o_school_sx_packages", "*", "sx_id='{$sx_id}' AND school_id='{$this->user_data['school_id']}'");
				$this->smarty->assign("pack_info", $pack_info);
				$school_order_info = get_field_by_where("o2o_school_order_records", "*", "school_id='{$this->user_data['school_id']}' AND data_id='{$sx_id}' AND data_type = 5 AND reviewed = 1 ORDER BY id DESC");
				$this->smarty->assign("school_order_info", $school_order_info);
			}
			$code_info = get_field_by_where("o2o_school_system_shixun_codes", "*", "school_id='{$this->user_data['school_id']}' AND sx_id='{$sx_id}' AND use_uid <= 0 AND status = 1 ORDER BY expire_day DESC LIMIT 1");
				$this->smarty->assign("code_info", $code_info);
			$this->smarty->assign("action", '授权');
			$this->smarty->assign("sx_id", $sx_id);
			$this->smarty->display("shixun/shelves.html");
		}
	}

	function shelves_down($pack_id){
		$this->db->where("id", $pack_id);
		$this->db->update("o2o_school_sx_packages", array("status" => 0));
		echo json_encode(array("code" => 1));
	}
}